package com.pulanit.pangu.admin.web.controller.business;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pulanit.pangu.admin.business.api.entity.Issue;
import com.pulanit.pangu.admin.business.api.service.IIssueService;
import com.pulanit.pangu.admin.business.api.param.IssuePageIn;
import cn.hutool.core.lang.Assert;
import com.gitee.pulanos.pangu.framework.sdk.dto.Result;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PageResult;
import com.pulanit.pangu.admin.common.AppContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PagingUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 问题反馈 前端控制器
 * </p>
 *
 * @author 天鸣
 * @since 2024-03-04
 */
@RestController
@RequestMapping("/api/business/issue")
public class IssueController {

    @Autowired
    private IIssueService issueService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public Result<PageResult<Issue>> list(IssuePageIn issuePageIn)
    {
        Page page = PagingUtil.createPage(issuePageIn);
        LambdaQueryWrapper<Issue> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(issuePageIn.getKeyword()), Issue::getIssueTitle, issuePageIn.getKeyword());
        issuePageIn.setIsAdmin(Boolean.FALSE);
        AppContext.getUserInfo().getRole().forEach(item->{
            if (StrUtil.equals("super",item)){
                issuePageIn.setIsAdmin(Boolean.TRUE);
            }
        });
        if (BooleanUtil.isFalse(issuePageIn.getIsAdmin())){
            wrapper.eq(Issue::getIssueUserId, AppContext.getUserInfo().getId());
        }
        Page<Issue> resultPage = issueService.page(page, wrapper);
        return Result.success(PagingUtil.transformPageResult(resultPage));
    }

    /**
    * 获取列表
    *
    * @return
    */
    @GetMapping("/list")
    public Result<List<Issue>> list() {
       List<Issue> list = issueService.list();
       return Result.success(list);
    }

    /**
     * 获取详情
     * @return
     */
    @GetMapping("/detail")
    public Result<Issue> detail(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        Issue issue = issueService.getById(id);
        return Result.success(issue);
    }

    /**
     * 保存
     * @param issue
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Issue issue){
        issue.setStatus(Boolean.FALSE);
        issue.setIssueUserId(AppContext.getUserInfo().getId());
        issue.setCreateDate(DateTime.now().toLocalDateTime());
        issue.setStatus(Boolean.FALSE);
        issueService.save(issue);
        return Result.success();
    }

    /**
     * 修改
     * @param issue
     * @return
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody Issue issue){
        issueService.updateById(issue);
        return Result.success();
    }

    /**
     * 提交已处理
     * @param id
     * @return
     */
    @PostMapping("/submit")
    public Result<Void> submit(@RequestParam("id") Long id){
        Issue issue = new Issue();
        issue.setId(id);
        issue.setStatus(true);
        issueService.updateById(issue);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        issueService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     */
     @PostMapping("/batchDelete")
     public Result<Void> batchDelete(@RequestParam List<Long> ids) {
        Assert.notEmpty(ids, "ID 不能为空");
        issueService.removeBatchByIds(ids);
        return Result.success();
     }

}
